﻿using System;
using System.Windows;
using ControleRepresentanteSync.Application.Setup;
using ControleRepresentanteSync.View.Wizard;

namespace ControleRepresentanteSync.View.Setup
{
    public class UserPageViewModel : WizardPageViewModelBase, IUserPageViewModel
    {
        public static readonly DependencyProperty IsUsernameValidProperty =
            DependencyProperty.Register("IsUsernameValid", typeof(bool), typeof(UserPageViewModel));
        public static readonly DependencyProperty IsPasswordValidProperty =
            DependencyProperty.Register("IsPasswordValid", typeof(bool), typeof(UserPageViewModel));
        public static readonly DependencyProperty UsernameProperty =
            DependencyProperty.Register("Username", typeof(string), typeof(UserPageViewModel), new PropertyMetadata(OnUsernameChanged));
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(UserPageViewModel), new PropertyMetadata(OnPasswordChanged));

        private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((UserPageViewModel) d).FirePasswordChanged();
        }

        private static void OnUsernameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((UserPageViewModel)d).FireUsernameChanged();
        }

        public UserPageViewModel()
            : base("Usuário", "Usuário e senha são requeridos para a comunicação com o servidor durante a sincronização.\nA autenticação garante uma forma segura de utilização do sistema.")
        {
        }

        private void FireUsernameChanged()
        {
            if (UsernameChanged != null)
                UsernameChanged(this, EventArgs.Empty);
        }

        private void FirePasswordChanged()
        {
            if (PasswordChanged != null)
                PasswordChanged(this, EventArgs.Empty);
        }

        public string Password
        {
            get { return (string) GetValue(PasswordProperty); }
            set { SetValue(PasswordProperty, value); }
        }

        public bool IsUsernameValid
        {
            get { return (bool) GetValue(IsUsernameValidProperty); }
            set { SetValue(IsUsernameValidProperty, value); }
        }

        public bool IsPasswordValid
        {
            get { return (bool) GetValue(IsPasswordValidProperty); }
            set { SetValue(IsPasswordValidProperty, value); }
        }

        public event EventHandler UsernameChanged;
        public event EventHandler PasswordChanged;

        public string Username
        {
            get { return (string) GetValue(UsernameProperty); }
            set { SetValue(UsernameProperty, value); }
        }
    }
}